package com.company.project.web;
import com.company.project.core.LayUiTableJson;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.SysFunction;
import com.company.project.service.SysFunctionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* Created by CodeGenerator on 2020/04/18.
*/
@RestController
@RequestMapping("/sys/function")
public class SysFunctionController {
    @Resource
    private SysFunctionService sysFunctionService;

    @PostMapping("/add")
    public Result add(SysFunction sysFunction) {
        sysFunctionService.save(sysFunction);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        sysFunctionService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SysFunction sysFunction) {
        sysFunctionService.update(sysFunction);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        SysFunction sysFunction = sysFunctionService.findById(id);
        return ResultGenerator.genSuccessResult(sysFunction);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SysFunction> list = sysFunctionService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/page/manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/function/functionList.html");
        return modelAndView;
    }

    @GetMapping("/page/detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/function/functionDetail.html");
        return modelAndView;
    }

    /**
     * 查询功能列表
     * @param functionType 功能类型
     * @return
     */
//    @ControllerAop(action = "查询功能列表")
    @RequestMapping("/queryFunctionList")
    public LayUiTableJson queryFunctionList(String functionName, int functionType){
        try{
            List<SysFunction> functions = sysFunctionService.queryFunctionByFunctionNameAndType(functionName, functionType);
            return new LayUiTableJson(0,"查询成功",functions.size(),functions);
        }catch (Exception e){
            e.printStackTrace();
            return new LayUiTableJson(1,"查询失败",0,null);
        }
    }

    /**
     * 查询功能列表
     * @param type
     * @param functionName
     * @return
     */
    @RequestMapping("/queryFunctionListTree")
    public LayUiTableJson queryFunctionListTree(Integer type, String functionName){
        try {
            List<SysFunction> functions = sysFunctionService.queryFunctionByFunctionNameAndType(functionName, type);
            List<HashMap> data = new ArrayList<HashMap>();
            for (SysFunction function : functions) {
                HashMap map = new HashMap();
                map.put("id",function.getId());
                map.put("name",function.getFunctionName());
                map.put("pId",function.getParentFunction());
                map.put("open",null);
                map.put("chkDisabled","false");
                data.add(map);
            }
            return new LayUiTableJson(0,null,data.size(),data);
        }catch (Exception e){
            e.printStackTrace();
            return new LayUiTableJson(1,"查询失败",0,null);
        }


    }
}
